/*
 * $LastChangedRevision: 40 $
 */
package controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import exception.ExceptionEcluse;
import model.components.Component;
import model.components.ComponentBox;
import utils.Constants;

public class EcluseController {
	private int mode = Constants.ECLUSE_PASSIVE_MODE;
	private FakeNetwork fnet;

	//private List<Component> componentList = new ArrayList<Component>();
	//private List<ComponentSensor> sensorList = new ArrayList<ComponentSensor>();
	
	private List<ComponentBox> playground = new ArrayList<ComponentBox>();

	//TODO: add playground with position
	
	public EcluseController(int pMode, FakeNetwork pFnet) {
		mode = pMode;
		fnet = pFnet;
		ComponentBox boxDown = new ComponentBox("BOXDOWN", Constants.COMPONENT_BOX_FREE);
		ComponentBox boxMiddle = new ComponentBox("BOXMIDDLE", Constants.COMPONENT_BOX_FREE);
		ComponentBox boxUp = new ComponentBox("BOXUP", Constants.COMPONENT_BOX_FREE);
		playground.add(boxDown);
		playground.add(boxMiddle);
		playground.add(boxUp);
		this.addComponent(boxDown);
		this.addComponent(boxMiddle);
		this.addComponent(boxUp);
	}

	/**
	 * @return the mode
	 */
	public int getMode() {
		return mode;
	}

	/**
	 * @param pMode
	 *            the mode to set
	 */
	public void setMode(int pMode) {
		mode = pMode;
	}

	public void addComponent(Object c) {
		fnet.connectComponent((Component) c);
	}



	public Component getLastAddedComponent() {
		return fnet.getLastAddedComponent();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	/*@Override
	public String toString() {
		return "EcluseServer [mode=" + mode + ", componentList=" + componentList + ",sensorList=" + sensorList + "]";
	}

	public void startAllComponent() {
		for (Component c : componentList) {
			c.start();
		}

	}*/

	public void initialize() throws ExceptionEcluse {
		// load the sqlite-JDBC driver using the current class loader
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}

		Connection connection = null;
		try {
			connection = DriverManager.getConnection("jdbc:sqlite:resources/ecluse.db");
			Statement statement = connection.createStatement();
			statement.setQueryTimeout(30); // set timeout to 30 sec.

			/* Load of components */
			ResultSet rs = statement.executeQuery("select * from components");
			while (rs.next()) {
				
				if (rs.getString("component_type").compareTo("DOOR") == 0) {
					this.addComponent(new ComponentDoorController(rs.getString("component_label"), Constants.COMPONENT_DOOR_CLOSED));

				} else if (rs.getString("component_type").compareTo("VALVE") == 0) {
					this.addComponent(new ComponentValveController(rs.getString("component_label"), Constants.COMPONENT_VALVE_CLOSED));

				} else if (rs.getString("component_type").compareTo("LIGHT") == 0) {
					this.addComponent(new ComponentLightController(rs.getString("component_label"), Constants.COMPONENT_LIGHT_RED));

				} else if (rs.getString("component_type").compareTo("SENSOR") == 0) {

					if (rs.getString("component_reference_type").compareTo("DOOR") == 0) {
						this.addComponent(new SensorDoorController(rs.getString("component_label"), this.mode, this.fnet
								.getComponentByLabel(rs.getString("component_reference_label"))));

					} else if (rs.getString("component_reference_type").compareTo("VALVE") == 0) {
						this.addComponent(new SensorValveController(rs.getString("component_label"), this.mode, this.fnet
								.getComponentByLabel(rs.getString("component_reference_label"))));

					} else if (rs.getString("component_reference_type").compareTo("LIGHT") == 0) {
						this.addComponent(new SensorLightController(rs.getString("component_label"), this.mode, this.fnet
								.getComponentByLabel(rs.getString("component_reference_label"))));
						
					} else if (rs.getString("component_reference_type").compareTo("MOVEMENT") == 0) {
						this.addComponent(new SensorMovementController(rs.getString("component_label"), this.mode, this.fnet
								.getComponentByLabel(rs.getString("component_reference_label"))));
						
					} else if (rs.getString("component_reference_type").compareTo("WATER") == 0) {
						this.addComponent(new SensorWaterController(rs.getString("component_label"), this.mode, this.fnet
								.getComponentByLabel(rs.getString("component_reference_label"))));
					} else {
						throw(new ExceptionEcluse("xx"));
					}
					
				}
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		} finally {
			try {
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				System.err.println(e);
			}
		}

	}
}
